﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace TrajectoryDetectorExplorer
{
    public partial class TrajectoryDetectorExplorer : Form
    {
        public TrajectoryDetectorExplorer()
        {
            InitializeComponent();
        }

        private void TrajectoryDetectorExplorer_Paint(object sender, PaintEventArgs e)
        {
            if (isPress)
            {
                drawLines(e);
            }
        }

        private void drawLines(PaintEventArgs e)
        {
            Pen blackPen = new Pen(Color.Blue, 1);
            Pen redPen = new Pen(Color.Red, 2);

            float scale = 600 / 90f;
            StreamReader reader = File.OpenText("E:\\dm data\\hurricane\\1990Out.txt");
            String line;
            String []token = new String[6];
            char []delim = new char[5];
            for (int i = 0; i < delim.Length; i++)
            {
                delim[i] = '\t';
            }

            bool startOfTra = true;
            String lastName = "";
            float fromX = 0.0f;
            float fromY = 0.0f;
            int num = 0;
            while ((line = reader.ReadLine()) != null)
            {
                token = line.Split(delim);
                if (token[1].Equals(lastName))
                {
                    startOfTra = false;
                }
                else
                {
                    startOfTra = true;
                    num++;
                }

                float toX = (float)Convert.ToDecimal(token[3]) * scale;
                float toY = (float)Convert.ToDecimal(token[4]) * scale;
                PointF p2 = new PointF(toX, toY);
                
                if (startOfTra == false)
                {
                    PointF p1 = new PointF(fromX, fromY);
                    if (token[5].Equals("0"))
                    {
                        e.Graphics.DrawLine(blackPen, p1, p2);
                    }
                    else
                    {
                        e.Graphics.DrawLine(redPen, p1, p2);
                    }

                }
                fromX = toX;
                fromY = toY;
                lastName = token[1];
            }
        }

        private void loadTrajectoriesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void loadTrajectoriesToolStripMenuItem_Paint(object sender, PaintEventArgs e)
        {
            this.isPress = true;
            drawLines(e);
        }

        private void buttonDetect_Click(object sender, EventArgs e)
        {
            float D = 4f;
            float p = 0.8f;
            float F = 0.1f;
            float Wpar = 0.1f;
            float Wper = 0.1f;
            float Wthe = 0.5f;
            bool success = detectOutlierTrajectories(D, p, F, Wpar, Wper, Wthe);

            if (success)
            {
                this.buttonDetect.Text = "Success!";
            }
        }

        private void buttonDraw_Paint(object sender, PaintEventArgs e)
        {
            drawLines(e);
        }

        [DllImport("TrajectoryDetection.dll", CharSet = CharSet.Unicode)]
        protected static extern bool detectOutlierTrajectories(float D, float p, float F, float Wpar, float Wper, float Wthe);

    }
}
